import java.io.File;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import com.ruoyi.system.service.IGuangDianUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 直接在后端读取CSV数据并导入的临时工具类
 */
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@ComponentScan(basePackages = {"com.ruoyi.system"})
public class ImportDataDirectly implements CommandLineRunner {
    
    private static final Logger logger = LoggerFactory.getLogger(ImportDataDirectly.class);
    
    @Autowired
    private IGuangDianUserService guangDianUserService;
    
    public static void main(String[] args) {
        logger.info("开始直接从后端导入CSV数据...");
        SpringApplication.run(ImportDataDirectly.class, args);
    }
    
    @Override
    public void run(String... args) throws Exception {
        try {
            // CSV文件路径，与GuangDianUserTask中配置保持一致
            String csvFilePath = "C:/Users/Administrator/RuoYi-Vue/guangdian-data-analysis/宽表数据.csv";
            
            // 检查文件是否存在
            File csvFile = new File(csvFilePath);
            if (!csvFile.exists()) {
                logger.error("CSV文件不存在：{}", csvFilePath);
                logger.info("请确保CSV文件已放置在正确路径");
                return;
            }
            
            logger.info("发现CSV文件：{}，文件大小：{}KB", csvFilePath, csvFile.length() / 1024);
            
            // 先导入2000条测试数据
            logger.info("开始导入测试数据(2000条)...");
            int testImported = guangDianUserService.importFromCsv(csvFilePath, 2000);
            logger.info("测试数据导入完成，成功导入 {} 条记录", testImported);
            
            // 询问是否继续导入全部数据
            logger.info("测试数据导入成功！");
            logger.info("仪表盘数据已准备就绪，可以查看统计信息");
            
        } catch (Exception e) {
            logger.error("数据导入过程中发生错误：", e);
            logger.info("请检查CSV文件格式和数据结构是否正确");
        } finally {
            logger.info("数据导入任务完成");
        }
    }
}